﻿using Microsoft.Xrm.Sdk;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Activities;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using Microsoft.Xrm.Sdk.Query;
using System.Diagnostics;

/**
 * Copyright 2011 by Alberto Gemin
 * agemin@hotmail.com
 * Version 1.7.0.1
 * 13 Dec 2011
 **/
namespace Gemina.CRM2011.WF.LdgManager
{
/*    [AgCodeActivity(ActivityNameMergeViewDefinitions, AgViewsUtils.GroupName)]
    public class AgMergeViewDefinitions : AgCodeActivityBase
    {

        #region Constants

        private const string ActivityNameMergeViewDefinitions = "Merge View Definitions";
        private const string Param1InputViewDefinitions1 = "View Definitions (Set 1)";
        private const string Param2InputViewDefinitions2 = "View Definitions (Set 2)";
        private const string Param1OutputMergedDefinitions = "Merged Definitions";
        private const string MsgNull2 = "{0} and {1} cannot be null.";

        #endregion

        #region Properties

        public List<Entity> ValidatedDefinitions1 { get; set; }
        public List<Entity> ValidatedDefinitions2 { get; set; }

        #endregion

        #region Workflow Input Parameters

        /// <summary>
        /// Input parameter
        /// </summary>
        [Input(Param1InputViewDefinitions1)]
        [RequiredArgument]
        public InArgument<String> InputViewDefinitions1 { get; set; }

        /// <summary>
        /// Input parameter
        /// </summary>
        [Input(Param2InputViewDefinitions2)]
        [RequiredArgument]
        public InArgument<String> InputViewDefinitions2 { get; set; }

        #endregion

        #region Workflow Output Parameters

        /// <summary>
        /// Output parameter
        /// </summary>
        [Output(Param1OutputMergedDefinitions)]
        public OutArgument<string> OutputMergedDefinitions { get; set; }

        #endregion

        #region CodeActivity

        override protected bool ExecuteBody(CodeActivityContext executionContext)
        {
            this.CheckArguments(executionContext);
            List<Entity> merged = this.MergeSets();

            this.OutputMergedDefinitions.Set(executionContext, this.Serialize(merged));

            return true;
        }

        private List<Entity> MergeSets()
        {
            Dictionary<String, Entity> set1 = this.LoadDictionary(this.ValidatedDefinitions1);
            Dictionary<String, Entity> set2 = this.LoadDictionary(this.ValidatedDefinitions2);
            foreach (String key in set1.Keys)
            {
                if (!set2.ContainsKey(key))
                {
                    Entity ent = set1[key];
                    set2.Add(key, ent);
                }
            }

            List<Entity> list = new List<Entity>();
            foreach (Entity ent in set2.Values)
            {
                list.Add(ent);
            }
            return list;
        }

        private Dictionary<string, Entity> LoadDictionary(List<Entity> list)
        {
            // this will remove duplicates randomly - too bad
            Dictionary<String, Entity> set = new Dictionary<string, Entity>();
            foreach (Entity ent in list)
            {
                string key = this.MakeKey(ent);
                if (!set.ContainsKey(key))
                {
                    set.Add(key, ent);
                }
            }
            return set;
        }

        private string MakeKey(Entity ent)
        {
            string name = (string)ent.Attributes[AgViewsUtils.FieldName];
            int? type = (int?)ent.Attributes[AgViewsUtils.FieldQueryType];
            string returned = (string)ent.Attributes[AgViewsUtils.FieldReturnedTypeCode];

            return
                (name == null ? String.Empty : name) + "|" +
                (type == null ? String.Empty : type.ToString()) + "|" +
                (returned == null ? String.Empty : returned);
        }

        #endregion

        #region Local Methods

        private void CheckArguments(CodeActivityContext executionContext)
        {
            string strDefinitions1 = this.InputViewDefinitions1.Get(executionContext);
            string strDefinitions2 = this.InputViewDefinitions2.Get(executionContext);
            if (String.IsNullOrEmpty(strDefinitions1) || String.IsNullOrEmpty(strDefinitions2))
            {
                throw new AgErrorMessage(String.Format(MsgNull2, Param1InputViewDefinitions1, Param2InputViewDefinitions2));
            }
            this.ValidatedDefinitions1 = this.DeserializeEntities(strDefinitions1);
            this.ValidatedDefinitions2 = this.DeserializeEntities(strDefinitions2);
        }

        #endregion

    }
    */
}
